/**
 * 考试相关API
 */
import { get, post, put, del, patch } from './index'
import type {
  ApiResponse,
  PaginatedResponse,
  PaginationParams,
  Examination,
  ExaminationSession,
  ExaminationAnswer,
  Question
} from '@/types'

// 考试管理
export const getExaminations = (params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<Examination>>>('/examinations/examinations/', params)
}

export const getExamination = (id: number) => {
  return get<ApiResponse<Examination>>(`/examinations/examinations/${id}/`)
}

export const createExamination = (data: Partial<Examination>) => {
  return post<ApiResponse<Examination>>('/examinations/examinations/', data)
}

export const updateExamination = (id: number, data: Partial<Examination>) => {
  return put<ApiResponse<Examination>>(`/examinations/examinations/${id}/`, data)
}

export const deleteExamination = (id: number) => {
  return del(`/examinations/examinations/${id}/`)
}

export const duplicateExamination = (id: number, data?: { title?: string; description?: string }) => {
  return post<ApiResponse<Examination>>(`/examinations/examinations/${id}/duplicate/`, data)
}

// 考试发布和管理
export const publishExamination = (id: number) => {
  return post<ApiResponse<Examination>>(`/examinations/examinations/${id}/publish/`)
}

export const startExamination = (id: number) => {
  return post<ApiResponse<Examination>>(`/examinations/examinations/${id}/start/`)
}

export const finishExamination = (id: number) => {
  return post<ApiResponse<Examination>>(`/examinations/examinations/${id}/finish/`)
}

export const unpublishExamination = (id: number) => {
  return post<ApiResponse<Examination>>(`/examinations/examinations/${id}/unpublish/`)
}

export const archiveExamination = (id: number) => {
  return post<ApiResponse<Examination>>(`/examinations/examinations/${id}/archive/`)
}

// 考试会话管理
export const getExaminationSessions = (examinationId?: number, params?: PaginationParams) => {
  const url = examinationId
    ? `/examinations/examinations/${examinationId}/sessions/`
    : '/examinations/sessions/'
  return get<ApiResponse<PaginatedResponse<ExaminationSession>>>(url, params)
}

export const getExaminationSession = (id: number) => {
  return get<ApiResponse<ExaminationSession>>(`/examinations/sessions/${id}/`)
}

export const createExaminationSession = (data: Partial<ExaminationSession>) => {
  return post<ApiResponse<ExaminationSession>>('/examinations/sessions/', data)
}

export const updateExaminationSession = (id: number, data: Partial<ExaminationSession>) => {
  return patch<ApiResponse<ExaminationSession>>(`/examinations/sessions/${id}/`, data)
}

export const deleteExaminationSession = (id: number) => {
  return del(`/examinations/sessions/${id}/`)
}

export const startExaminationSession = (sessionId: number) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/start/`)
}

export const endExaminationSession = (sessionId: number) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/end/`)
}

// 学生管理相关
export const getExamStudents = (examinationId: number, params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<any>>>(`/examinations/examinations/${examinationId}/students/`, params)
}

export const addStudentsToExam = (examinationId: number, data: { student_ids: number[] }) => {
  return post<ApiResponse<any>>(`/examinations/examinations/${examinationId}/students/add/`, data)
}

export const removeStudentsFromExam = (examinationId: number, data: { student_ids: number[] }) => {
  return post<ApiResponse<any>>(`/examinations/examinations/${examinationId}/students/remove/`, data)
}

export const updateExamStudents = (examinationId: number, data: any) => {
  return put<ApiResponse<any>>(`/examinations/examinations/${examinationId}/students/`, data)
}

// 学生考试相关
export const joinExamination = (examinationId: number) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/examinations/${examinationId}/join/`)
}

export const startExamSession = (sessionId: number) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/start/`)
}

export const submitExamSession = (sessionId: number, data: { answers: any[] }) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/submit/`, data)
}

export const getExamQuestions = (sessionId: number) => {
  return get<ApiResponse<Question[]>>(`/examinations/sessions/${sessionId}/questions/`)
}

// 考试答案管理
export const getExaminationAnswers = (sessionId: number) => {
  return get<ApiResponse<ExaminationAnswer[]>>(`/examinations/sessions/${sessionId}/answers/`)
}

export const saveAnswer = (sessionId: number, data: {
  question: number
  answer?: any
  text_answer?: string
  time_spent?: number
}) => {
  return post<ApiResponse<ExaminationAnswer>>(`/examinations/sessions/${sessionId}/answers/`, data)
}

export const updateAnswer = (answerId: number, data: Partial<ExaminationAnswer>) => {
  return patch<ApiResponse<ExaminationAnswer>>(`/examinations/answers/${answerId}/`, data)
}

// 监考相关
export const getMonitoringData = (examinationId: number) => {
  return get<ApiResponse<any>>(`/examinations/examinations/${examinationId}/monitoring/`)
}

export const startMonitoring = (examinationId: number) => {
  return post<ApiResponse<any>>(`/examinations/examinations/${examinationId}/start-monitoring/`)
}

export const flagSuspiciousActivity = (sessionId: number, data: {
  reason: string
  evidence?: string
}) => {
  return post<ApiResponse<any>>(`/examinations/sessions/${sessionId}/flag-suspicious/`, data)
}

export const handleViolation = (sessionId: number, data: {
  action: string
  reason: string
  evidence?: string
}) => {
  return post<ApiResponse<any>>(`/examinations/sessions/${sessionId}/handle-violation/`, data)
}

export const getCheatingAlerts = (examinationId: number) => {
  return get<ApiResponse<any[]>>(`/examinations/examinations/${examinationId}/cheating-alerts/`)
}

// 考试统计
export const getExaminationStatistics = (examinationId: number) => {
  return get<ApiResponse<any>>(`/examinations/examinations/${examinationId}/statistics/`)
}

export const getSessionStatistics = (sessionId: number) => {
  return get<ApiResponse<any>>(`/examinations/sessions/${sessionId}/statistics/`)
}

// 考试结果
export const getExamResults = (examinationId: number, params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<any>>>(`/examinations/examinations/${examinationId}/results/`, params)
}

export const getStudentExamResults = (studentId?: number, params?: PaginationParams) => {
  const url = studentId
    ? `/examinations/students/${studentId}/results/`
    : '/examinations/my-results/'
  return get<ApiResponse<PaginatedResponse<any>>>(url, params)
}

export const exportExamResults = (examinationId: number, format: 'excel' | 'csv' = 'excel') => {
  return post(`/examinations/examinations/${examinationId}/export-results/`, { format })
}

// 考试配置
export const getExamSettings = (examinationId: number) => {
  return get<ApiResponse<any>>(`/examinations/examinations/${examinationId}/settings/`)
}

export const updateExamSettings = (examinationId: number, data: any) => {
  return put<ApiResponse<any>>(`/examinations/examinations/${examinationId}/settings/`, data)
}

// 考试日志
export const getExamLogs = (examinationId: number, params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<any>>>(`/examinations/examinations/${examinationId}/logs/`, params)
}

export const getSessionLogs = (sessionId: number) => {
  return get<ApiResponse<any[]>>(`/examinations/sessions/${sessionId}/logs/`)
}

// 时间管理
export const extendSessionTime = (sessionId: number, data: { extra_minutes: number; reason: string }) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/extend-time/`, data)
}

export const pauseSession = (sessionId: number, data: { reason: string }) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/pause/`, data)
}

export const resumeSession = (sessionId: number) => {
  return post<ApiResponse<ExaminationSession>>(`/examinations/sessions/${sessionId}/resume/`)
}

// 导出统一的考试API对象
export const examinationsApi = {
  // 考试管理
  getExaminations,
  getExamination,
  createExamination,
  updateExamination,
  deleteExamination,
  duplicateExamination,

  // 考试发布和管理
  publishExamination,
  unpublishExamination,
  archiveExamination,

  // 考试会话管理
  getExaminationSessions,
  createExaminationSession,
  getExaminationSession,
  updateExaminationSession,
  deleteExaminationSession,
  startExaminationSession,
  endExaminationSession,

  // 学生管理
  getExamStudents,
  addStudentsToExam,
  removeStudentsFromExam,
  updateExamStudents,

  // 监考管理
  startMonitoring,
  getMonitoringData,
  handleViolation,
  getSessionLogs,

  // 时间管理
  extendSessionTime,
  pauseSession,
  resumeSession
}